package com.qf.hotel.controller.foodtype;

import com.qf.hotel.pojo.FoodType;
import com.qf.hotel.service.FoodTypeService;
import com.qf.hotel.service.impl.FoodTypeServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/foodtype/update")
public class FoodTypeUpdateController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 字符编码设置
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        try {
            // 收参
            Map<String, String[]> parameterMap = request.getParameterMap();

            // 创建foodType对象
            FoodType foodType = new FoodType();

            // 自动参数填充--自动调用set方法设置属性--参数名称和属性名完全一样
            BeanUtils.populate(foodType,parameterMap);

            // 创建业务对象，执行修改
            FoodTypeService foodTypeService = new FoodTypeServiceImpl();
            int i = foodTypeService.update(foodType);

            // 跳转到查询的控制层
            response.sendRedirect(request.getContextPath() + "/foodtype/search");

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
